//
//  LatestBespeakTvCell.swift
//  Venus
//
//  Created by apple on 2018/9/3.
//  Copyright © 2018年 zhaowang. All rights reserved.
//  最新预约cell - 机构端

import UIKit

protocol LatestBespeakTvCellDelegate : class{
    
    /// 点击电话
    func clickCellPhone(row:Int)
    
    
    /// 点击记录
    func clickCellRecord(row:Int)
    
}
class LatestBespeakTvCell: UITableViewCell {
    
    
    /// 代理
    weak var  delegate: LatestBespeakTvCellDelegate?
    
    /// 头像
    private lazy var headerIv : UIImageView = {
        let iv = UIImageView()
        iv.translatesAutoresizingMaskIntoConstraints = false
        iv.image = UIImage.init(named: "bespeak_user")
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 25
        return iv
    }()
    
    /// 用户名称
    private lazy var userNameLabel : UILabel = {
        let lab = UILabel(name: "放羊的娃娃", color: UIColor.init(r: 51, g: 51, b: 51), fontSize: 17, isBoldFont:true)
        lab.translatesAutoresizingMaskIntoConstraints = false
        return lab
    }()
    
    
    /// 日期label
    private lazy var dateLabel : UILabel = {
        let lab = UILabel(name: "2017-8-21", color: UIColor.init(r: 153, g: 153, b: 153), fontSize: 12)
        lab.translatesAutoresizingMaskIntoConstraints = false
        return lab
    }()
    
    
    /// 电话按钮
    private lazy var phoneBtn : UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setImage(UIImage.init(named: "bespeak_phone"), for: .normal)
        btn.addTarget(self, action: #selector(clickPhoneBtn), for: .touchUpInside)
    
        btn.layer.masksToBounds = true
        //btn.setTitle("预约", for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        return btn
    }()
    
    /// 记录按钮
    private lazy var recordBtn : UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setImage(UIImage.init(named: "bespeak_record"), for: .normal)
        //btn.setTitle("记录", for: UIControlState.normal)

        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(clickRecordBtn), for: .touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        return btn
    }()
    
    
    /// 分割线
    private lazy var lineView : UIView = {
        let v = UIView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = UIColor.lightGray
        return v
    }()
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    
    /// 配置数据
    func configData(indexpath:IndexPath,
                    delegate:LatestBespeakTvCellDelegate, data:BespeakStruct){
        self.tag = indexpath.row
        self.delegate = delegate
        //网络下载
        self.headerIv.showImageI(data.headImgStr)
        self.userNameLabel.text = data.userName
        self.dateLabel.text = data.dateStr
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    
    /// 点击电话
    @objc func clickPhoneBtn(){
        if delegate != nil{
            delegate?.clickCellPhone(row: self.tag)
        }
    }
    
    /// 点击记录
    @objc func clickRecordBtn(){
        if delegate != nil{
            delegate?.clickCellRecord(row: self.tag)
        }
    }
}

extension LatestBespeakTvCell{
    func setupUI() {
        self.selectionStyle = .none
        let vd : [String:UIView] = ["headerIv":headerIv,
                                    "userNameLabel":userNameLabel,
                                    "dateLabel":dateLabel,
                                    "phoneBtn":phoneBtn,
                                    "recordBtn":recordBtn,
                                    "lineView":lineView]
        contentView.addSubview(headerIv)
        contentView.addSubview(userNameLabel)
        contentView.addSubview(dateLabel)
        contentView.addSubview(recordBtn)
        contentView.addSubview(phoneBtn)
        contentView.addSubview(lineView)
        lineView.backgroundColor = nC1
        
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"|-10-[headerIv(50)]-11-[userNameLabel][phoneBtn(25)]-30-[recordBtn(==phoneBtn)]-14-|", options: [], metrics: nil, views: vd))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"|[lineView]|", options: [], metrics: nil, views: vd))
        
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"V:|-10-[headerIv(50)]-9-[lineView(1)]|", options: [], metrics: nil, views: vd))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"V:|-20-[userNameLabel]-10-[dateLabel]-20-|", options: [.alignAllLeading], metrics: nil, views: vd))
        
        recordBtn.heightAnchor.constraint(equalToConstant: 25).isActive = true
        phoneBtn.heightAnchor.constraint(equalToConstant: 25).isActive = true
        recordBtn.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        phoneBtn.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true

    }
}
